#include <logbase.h>
#include <errno.h>
#include <stdarg.h>
#include <cstring.h>
#include <crcCalc.h>
#include <genrand.h>
#include <typecvrt.h>
#include <hexstr.h>

#define swapInStcDef(dname) swapInStc_##dname
#define swapOutStcDef(dname) swapOutStc_##dname
#define swapSwapStcDef(dname) swapSwapStc_##dname
#define swapFuncDef(dname) swapFunc_##dname
#define swapIOvtDef(dname) swapIOvt_##dname
#define swapOOvtDef(dname) swapOOvt_##dname
#define swapEnumDef(dname) swapEnum_##dname

typedef void (*iOvtFuncPtr)(void *);
typedef void (*oOvtFuncPtr)(void *);
typedef int32_t (*swapFuncPtr)(void *);
struct swapOptStc
{
    iOvtFuncPtr iovt;
    oOvtFuncPtr oovt;
    swapFuncPtr sfunc;
};

#define swapOptDtDef(dname)                                        \
    {                                                              \
        swapIOvtDef(dname), swapOOvtDef(dname), swapFuncDef(dname) \
    }

enum swapCmdRstEnum
{
    swapRst_OK = 0,
    swapRst_InNone = -254,
    swapRst_VtfErr, // vertify error
};

//====================================1
#pragma pack(1)
struct swapInStcDef(baseInf)
{
    char rand[16];
    uint16_t icrc;
};
struct swapOutStcDef(baseInf)
{
    char ver[32];
    char sn[16];
    uint16_t ocrc;
};
union swapSwapStcDef(baseInf)
{
    struct swapInStcDef(baseInf) idt;
    struct swapOutStcDef(baseInf) odt;
};
#pragma pack()
#define swapOptVerStr "sub-std.v1.1.2407051751"
#define swapOptSnStr "jflaeicjla471hnl"
void swapIOvtDef(baseInf)(void *idt)
{
    struct swapInStcDef(baseInf) *tmpidt = (struct swapInStcDef(baseInf) *)idt;
    tmpidt->icrc = ovtBigSys16(tmpidt->icrc);
    // tmpidt->icrc = ovtLitSys16(tmpidt->icrc);
}
void swapOOvtDef(baseInf)(void *odt)
{
    struct swapOutStcDef(baseInf) *tmpodt = (struct swapOutStcDef(baseInf) *)odt;
    tmpodt->ocrc = ovtBigSys16(tmpodt->ocrc);
    // tmpodt->ocrc = ovtLitSys16(tmpodt->ocrc);
}
int32_t swapFuncDef(baseInf)(void *sdt)
{
    union swapSwapStcDef(baseInf) *tmpswp = (union swapSwapStcDef(baseInf) *)sdt;
    if (!tmpswp)
    {
        logerrorc("");
        return swapRst_InNone;
    }
    logdebugc("%.*s,0x%x", (int)sizeof(tmpswp->idt.rand), tmpswp->idt.rand, tmpswp->idt.icrc);
    uint16_t tmpcrc = calcSumCrc16((const uint8_t *)tmpswp->idt.rand, sizeof(tmpswp->idt.rand));
    if (tmpcrc != tmpswp->idt.icrc)
    {
        logerrorc("%x!=%x", tmpcrc, tmpswp->idt.icrc);
        return swapRst_VtfErr;
    }
    tmpswp->odt.ocrc = calcHashCrc16((const uint8_t *)tmpswp->idt.rand, sizeof(tmpswp->idt.rand));
    strcpy(tmpswp->odt.ver, swapOptVerStr);
    memcpy(tmpswp->odt.sn, swapOptSnStr, strlen(swapOptSnStr));
    logdebugc("func out=%x", tmpswp->odt.ocrc);
    return swapRst_OK;
}

//====================================2
#pragma pack(1)
struct swapInStcDef(heartbeat)
{
    uint32_t hbint;
    float hbflt;
    char reserve[4];
};
struct swapOutStcDef(heartbeat)
{
    uint16_t hb16;
    float hbflt;
    char reserve[4];
};
union swapSwapStcDef(heartbeat)
{
    struct swapInStcDef(heartbeat) idt;
    struct swapOutStcDef(heartbeat) odt;
};
#pragma pack()
#define swapOptVerStr "sub-std.v1.1.2407051751"
#define swapOptSnStr "jflaeicjla471hnl"
void swapIOvtDef(heartbeat)(void *idt)
{
    struct swapInStcDef(heartbeat) *tmpidt = (struct swapInStcDef(heartbeat) *)idt;
    tmpidt->hbint = ovtBigSys32(tmpidt->hbint);
    tmpidt->hbflt = ovtBigSysFloat(tmpidt->hbflt);
}
void swapOOvtDef(heartbeat)(void *odt)
{
    struct swapOutStcDef(heartbeat) *tmpodt = (struct swapOutStcDef(heartbeat) *)odt;
    tmpodt->hb16 = ovtBigSys16(tmpodt->hb16);
    tmpodt->hbflt = ovtBigSysFloat(tmpodt->hbflt);
}
int32_t swapFuncDef(heartbeat)(void *sdt)
{
    union swapSwapStcDef(heartbeat) *tmpswp = (union swapSwapStcDef(heartbeat) *)sdt;
    if (!tmpswp)
    {
        logerrorc("");
        return swapRst_InNone;
    }
    logdebugc("[%s]:%u,%f", __func__, tmpswp->idt.hbint, tmpswp->idt.hbflt);
    tmpswp->odt.hb16 = rand();
    tmpswp->odt.hbflt = rand();
    logdebugc("[%s]:%u,%f", __func__, tmpswp->odt.hb16, tmpswp->odt.hbflt);
    return swapRst_OK;
}

struct swapOptStc swpOptList[] = {
    swapOptDtDef(baseInf),
    swapOptDtDef(heartbeat),
};
struct myChkB;
struct myChkA
{
    struct myChkB *val;
};
struct myChkB
{
    struct myChkA *val;
};
#include <time2date.h>


struct my_check_stc1
{
    int x;
    int y;
    int z;
};

int main()
{
    {
        struct my_check_stc1 tmpa;
        logdebugc("%d,%d,%d", tmpa.x, tmpa.y, tmpa.z);
    }
    {
        struct my_check_stc1 tmpa = {};
        logdebugc("%d,%d,%d", tmpa.x, tmpa.y, tmpa.z);
    }
    {
        char tmp1[16], tmp2[16];
        // sscanf("LPT-AP-STD-V1.1.2503300014", "%s-%s-", tmp1, tmp2);
        sscanf("LPT-AP-STD-V1.1.2503300014", "%[^-]-%[^-]", tmp1, tmp2);
        logdebugc("%s,%s", tmp1, tmp2);
    }
    {
        tmdateType tmpdate = stamp2date(1726125396, 0);
        logdebugc("%d-%d-%d %d:%d:%d", tmpdate.year, tmpdate.month, tmpdate.day, tmpdate.hour, tmpdate.minute, tmpdate.second);
    }
    {
        typedef struct
        {
            char v1;
            short v2;
            char v3;
            int v4;
        } mychkDuiqi;
        logdebugc("myduiqi:%d", (int)sizeof(mychkDuiqi));
    }
    {
        typedef struct
        {
            char v1;
            short v2;
            char v3;
            int v4;
        } mychkDuiqi;
        char tmpbuf[32] = {};
        mychkDuiqi *tmpptr = (mychkDuiqi *)(tmpbuf + 1);
        logdebugc("%p", tmpptr);
        tmpptr->v1 = 1;
        tmpptr->v2 = 2;
        tmpptr->v3 = 3;
        tmpptr->v4 = 4;
        {
            char tmpHexstr[sizeof(tmpbuf) * 2 + 1];
            cvtHex2str(tmpbuf, sizeof(tmpbuf), tmpHexstr, sizeof(tmpHexstr));
            logdebugc("%s", tmpHexstr);
        }
    }
    {
        char tmpstr1[16] = "123456\0\1";
        char tmpstr2[sizeof(tmpstr1)] = "123456\0\2";
        logdebugc("cmp=%d", strncmp(tmpstr1, tmpstr2, sizeof(tmpstr1)));
        logdebugc("cmp=%d", memcmp(tmpstr1, tmpstr2, sizeof(tmpstr1)));
        int32_t tmp32_1 = 1, tmp32_2 = 2;
        int64_t tmpcmpflag = tmp32_1 - tmp32_2;
        logdebugc("cmp=%ld", tmpcmpflag);
    }

    char tmpswp[128];
    struct swapInStcDef(baseInf) *tmpinf = (struct swapInStcDef(baseInf) *)tmpswp;
    struct swapOutStcDef(baseInf) *tmpout = (struct swapOutStcDef(baseInf) *)tmpswp;
    genRandStrSpec(tmpinf->rand, sizeof(tmpinf->rand));
    tmpinf->icrc = calcSumCrc16((const uint8_t *)tmpinf->rand, sizeof(tmpinf->rand));
    swpOptList[0].iovt(tmpswp);
    logdebugc("i1crc = %x", tmpinf->icrc);
    swpOptList[0].iovt(tmpswp);
    swpOptList[0].sfunc(tmpswp);
    swpOptList[0].oovt(tmpswp);
    logdebugc("o1crc = %x", tmpout->ocrc);
    swpOptList[0].oovt(tmpswp);
    logdebugc("%s,%.*s,%x", tmpout->ver, (int)sizeof(tmpout->sn), tmpout->sn, tmpout->ocrc);

    struct swapInStcDef(heartbeat) *tmphbi = (struct swapInStcDef(heartbeat) *)tmpswp;
    struct swapOutStcDef(heartbeat) *tmphbo = (struct swapOutStcDef(heartbeat) *)tmpswp;
    tmphbi->hbint = rand();
    tmphbi->hbflt = rand();
    logdebugc("[%s]:%u,%f", __func__, tmphbi->hbint, tmphbi->hbflt);
    swpOptList[1].sfunc(tmpswp);
    logdebugc("[%s]:%u,%f", __func__, tmphbo->hb16, tmphbo->hbflt);
    return 0;
}
